package com.shxc.smarttrade.old.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.MasterTrade;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/10.
 */

public class SupermanTradeHistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    LayoutInflater mInflater;
    Context mContext;
    List<MasterTrade.DataBean.TradesBean> tradesBeanList;


    public SupermanTradeHistoryAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setDatas(List<MasterTrade.DataBean.TradesBean> tradesBeanList) {
        this.tradesBeanList = tradesBeanList;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new AllPositionViewholder(mInflater.inflate(R.layout.adapter_superman_trade_history, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MasterTrade.DataBean.TradesBean tb = tradesBeanList.get(position);
        if (holder instanceof AllPositionViewholder) {
            if (tb.getCmd() == 0) {
                ((AllPositionViewholder) holder).tvCmd.setText("涨");
                ((AllPositionViewholder) holder).tvCmd.setBackgroundResource(R.drawable.blue_text_bg2);
            } else {
                ((AllPositionViewholder) holder).tvCmd.setText("跌");
                ((AllPositionViewholder) holder).tvCmd.setBackgroundResource(R.drawable.yellow_text_bg2);
            }

            ((AllPositionViewholder) holder).tvName.setText(tb.getSymbolCn());
            long time = tb.getPassedTime();
            if (tb.getCloseType() == -1) {
                ((AllPositionViewholder) holder).tvTime.setText("持仓 " + countTime(time, ""));
            } else {
                ((AllPositionViewholder) holder).tvTime.setText(tb.getCloseTypeName() + " " + countTime(time, "前"));
                ((AllPositionViewholder) holder).tvPosPrics.setText("平仓价");
            }
            ((AllPositionViewholder) holder).tvOpenPrice.setText(tb.getOpenPrice() + "");
            ((AllPositionViewholder) holder).tvClosePrice.setText(tb.getClosePrice() + "");
            if (tb.getProfit() > 0)
                ((AllPositionViewholder) holder).tvProfit.setTextColor(mContext.getResources().getColor(R.color.stock_rise_color));
            else
                ((AllPositionViewholder) holder).tvProfit.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
            ((AllPositionViewholder) holder).tvProfit.setText(BaseUtils.dealSymbol(tb.getProfit()));

        }
    }

    @Override
    public int getItemCount() {
        return tradesBeanList == null ? 0 : tradesBeanList.size();
    }

    public class AllPositionViewholder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_cmd)
        TextView tvCmd;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_open_price)
        TextView tvOpenPrice;
        @BindView(R.id.tv_close_price)
        TextView tvClosePrice;
        @BindView(R.id.tv_profit)
        TextView tvProfit;
        @BindView(R.id.tv_pos_price)
        TextView tvPosPrics;

        AllPositionViewholder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }

    private String countTime(long time, String suf) {
//        int minute = (int) time / 60;//分钟
//        int hour = (int) time / 3600;//小时
//        int day = (int) (time / 3600 * 24);//天
//        int week = (int) (time / 3600 * 24 * 7);//周
//        int month = (int) (time / 3600 * 24 * 7 * 30);//月
//        int year = (int) (time / 3600 * 24 * 7 * 30 * 12);//年
        int newTime = 0;
        int dayTime = 0;
        if ((newTime = (int) time / 60) > 60) {
            if ((newTime = (int) time / 3600) > 24) {
                if ((dayTime = (int) (time / (3600 * 24))) > 30) {
                    if ((newTime = (int) (time / (3600 * 24 * 30))) > 12) {
                        return 1 + "年" + suf;
                    } else {
                        return newTime + "月" + suf;
                    }
                } else {
                    if ((newTime = (int) (time / (3600 * 24 * 7))) >= 1) {
                        return newTime + "周" + suf;
                    } else
                        return dayTime + "天" + suf;
                }
            } else {
                return newTime + "小时" + suf;
            }

        } else {
            if (newTime == 0)
                return "刚刚";
            else
                return newTime + "分钟" + suf;
        }
    }
}
